// pages/biz/my/customDetail/customDetail.js
//引入请求接口文件
import Serv from './customDetailServ';
var cf = require("../../../../utils/config");
var regeneratorRuntime = require("../../../../lib/js/runtime-module.js")

Page({

  /**
   * 页面的初始数据
   */
  data: {
    statusAttr: [
      {
        statusName: '已提交',
        createTime: '',
        status: 0,
        iconCla: 'iconfont icon-icon_label_kehuxiangqing_tijiao'
      },
      {
        statusName: '已跟进',
        createTime: '',
        status: 1,
        iconCla: 'iconfont icon-icon_label_kehuxiangqing_genjin'
      },
      {
        statusName: '已到访',
        createTime: '',
        status: 2,
        iconCla: 'iconfont icon-icon_label_kehuxiangqing_daofang'
      },
      {
        statusName: '已认筹',
        createTime: '',
        status: 3,
        iconCla: 'iconfont icon-icon_label_kehuxiangqing_renchou'
      },
      {
        statusName: '已认购',
        createTime: '',
        status: 4,
        iconCla: 'iconfont icon-icon_label_kehuxiangqing_rengou'
      },
      {
        statusName: '已签约',
        createTime: '',
        status: 5,
        iconCla: 'iconfont icon-icon_label_kehuxiangqing_qianyue'
      },
      {
        statusName: '已计佣',
        createTime: '',
        status: 6,
        iconCla: 'iconfont icon-icon_label_kehuxiangqing_jiyong'
      },
      {
        statusName: '已发佣',
        createTime: '',
        status: 7,
        iconCla: 'iconfont icon-icon_label_kehuxiangqing_fayong'
      },
    ],
    statusIdx: 0,
    customDetail: {},
    followUpList: [],
    type: 0,
    poster: '', //http://img02.tooopen.com/images/20150928/tooopen_sy_143912755726.jpg
    estatePhone: '',
    btnStatus:'',
    evaluateBtn: true,
    sex:"",
    status:""

  },
  makeCall: function(e) {
    let phone = e.currentTarget.dataset.phone
    wx.makePhoneCall({
      phoneNumber: phone,
      success: function(res) {},
      fail: function(res) {},
      complete: function(res) {},
    })
  },
  makePhone: function(e) {
    let phone = e.currentTarget.dataset.phone
    wx.makePhoneCall({
      phoneNumber: phone,
      success: function(res) {},
      fail: function(res) {},
      complete: function(res) {},
    })
  },
  //跳转到推荐页
  routerShare: function(e) {
    let id = e.currentTarget.dataset.productid,
    clueId = e.currentTarget.dataset.clueid

    wx.navigateTo({
      url: `../../recommend/recommend?id=${id}&clueId=${clueId}`,
    })
  },
  toDetail: function (e) {
    console.log('toDetail', e)
    if ('detailLink' != e.currentTarget.id) {
      return false
    }
    let sex = this.data.sex;
    console.log("sex:" + sex);
    let status = this.data.status;
    console.log("status:" + status);
    let customDetail = this.data.customDetail;
    console.log("name:"+customDetail.name)
    let { counselorId, productId, counselorName, evaluateId, productName } = customDetail;
    
    // let customDetail = this.data.appointList.customDetail;
    wx.navigateTo({
      url: `../../evaluate/evaluateConsultant/evaluateConsultant?counselorId=${counselorId}&productId=${productId}&evaluateId=${evaluateId}&productName=${productName}&sex=${sex}&status=${status}&counselorName=${counselorName}&prevpage=1`

    })
  },
  toEvalute: function (e) {
    console.log('toEvalute', e)
    if ('evaluateLink' != e.currentTarget.id) {
      return false
    }
    let customDetail = this.data.customDetail;
    let { counselorId, productId, counselorName, evaluateId, evaluateable, productName } = customDetail;
    let sex = this.data.sex;
    let status = this.data.status;
    
    if (evaluateable){
      wx.redirectTo({
        url: `/subPackages/pages/evaluate/evaluateConsultant/evaluateConsultant?counselorId=${counselorId}&productId=${productId}&evaluateId=${evaluateId}&productName=${productName}&sex=${sex}&status=${status}&counselorName=${counselorName}&evaluateable=${evaluateable}`,
      });
    } else {
      wx.showToast({
        title: '您现在还不能评价哦~',
        icon: 'none',
        duration: 2000
      })
    }
  },
  //获取置业神器顾问信息-手机号
  getAdviser(){
    let cusmallToken = wx.getStorageSync('cusmallToken')
    wx.request({
      url:cf.config.pageDomain+`applet/mobile/visitcard/employee/getEmployeeInfo`,
      data:{cusmallToken},
      header: {
        'content-type': 'application/json'
      },
    })
  },
  //获取客户详情
  async getCustomDetail(params) {
    let {
      statusAttr
    } = this.data;
    let {
      data
    } = await Serv.customDetailGet(params)
    console.log(data)
    statusAttr.map((item,idx)=>{
      data.followUpList[idx]? data.followUpList[idx].iconCla = item.iconCla : null
    })
    let followUpList = data.followUpList ? [].concat(data.followUpList, statusAttr.splice(data.followUpList.length)) : statusAttr;
    !data.followUpList && (statusAttr[0].createTime = data.createTime)
     console.log(followUpList)
    this.setData({
      customDetail: data,
      followUpList,
      statusIdx: data.followUpList ? data.followUpList.length - 1 : 0,
      statusAttr
    })
    console.log(this.data.customDetail)
    //获取图片
    this.getEstateDetail(data.productId);
  },
  //获取美房图片
  async getEstateDetail(productId) {
    let {
      data
    } = await Serv.queryEstateDetail(productId)
    if (data.medias && data.medias.poster) {
      this.setData({
        poster: data.medias.effect[0],
        estateDetail : data
      })
    }
    if (data.estatePhone) {
      this.setData({
        estatePhone: data.estatePhone
      })
    }
    // console.log(data.medias, "data33333")
  },
  //获取预约详情
  async queryMyAppointMentFun(params) {
    let {
      statusAttr
    } = this.data;
    let {
      data,
      success
    } = await Serv.queryMyAppointMent(params)
    let customDetail = {}
    let followUpList = []
    statusAttr.map((item, idx) => {
      data.followUpList[idx] ? data.followUpList[idx].iconCla = item.iconCla : null
      
    })
    if (success) {
      console.log(data)
      customDetail = data
      followUpList = data.followUpList ? [].concat(data.followUpList, statusAttr.splice(data.followUpList.length)) : statusAttr;
      !data.followUpList && (statusAttr[0].createTime = data.createTime)
    }
    console.log(followUpList)
    this.setData({
      followUpList,
      statusIdx: data.followUpList ? data.followUpList.length - 1 : 0,
      customDetail
    })
    this.getEstateDetail(data.productId)
    console.log("customDetail:" + customDetail.evaluateId);
    let btnStatus = customDetail.btnStatus = customDetail.evaluateId ? 1 : 2;
    console.log("btnStatus:" + btnStatus);
    this.setData({
      btnStatus: btnStatus
    });
    
  },
  
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    let { sex, status } = options;
    let { statusAttr} =this.data
    this.setData({
      optionsPersonal: options,
      statusAttrCody: statusAttr
    });
    // let sex = options.sex;
    // let status = options.status;
    // options = this.options;
    //获取客户详情
    let type = options.type || ''
    if (type == 'personal') { // 从个人页跳转
      wx.setNavigationBarTitle({
        title: '预约详情'
      })
      this.setData({
        type: 0,
        clueId: options.id
      })
      let params = {
        clueId: options.id
      }
    
      
      this.queryMyAppointMentFun(params)
    } else {
      wx.setNavigationBarTitle({
        title: '客户详情'
      })
      this.setData({
        type: 1
      })

      this.getCustomDetail({
        clueId: options.id
      })
    }
    this.setData({
      sex:sex,
      status: status
    });
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },
  // onlyqueryMyAppointMentFun: async function (params){
  //   let {
  //     data,
  //     success
  //   } = await Serv.queryMyAppointMent(params)
  //   if(success){
  //     this.setData({
  //       sex: data.sex,
  //       status: data.status
  //     });
  //   }
  // },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

    // let { clueId} = this.data
    
    // let params = {
    //   clueId
    // }
    // this.onlyqueryMyAppointMentFun(params)

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})